package com.heer.gymnasium.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.MediaCodec;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.imagepick.DensityUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yan on 2018/1/18.
 * 标题工具类
 */

public class TitleUtils {
    private static final int LEFT_LL_ID = R.id.layout_title_bar_left_ll;
    private static final int LEFT_IV_ID = R.id.layout_title_bar_left_iv;
    private static final int LEFT__TV_ID = R.id.layout_title_bar_left_tv;
    private static final int RIGHT_ID = R.id.layout_title_bar_right_tv;
    private static final int TITLE_ID = R.id.layout_title_bar_title_tv;
    private static final int LINE_ID = R.id.layout_title_bar_line_v;
    private static final int BARVIEW_ID = R.id.layout_title_bar_rl;
    @BindView(LEFT_LL_ID)
    LinearLayout leftLinearLayout;
    @BindView(LEFT_IV_ID)
    ImageView leftImageView;
    @BindView(TITLE_ID)
    TextView titleTextView;
    @BindView(LEFT__TV_ID)
    TextView leftTextView;
    @BindView(RIGHT_ID)
    TextView rightTextView;
    @BindView(LINE_ID)
    View lineView;
    @BindView(BARVIEW_ID)
    View titleBarView;


    public TitleUtils(Activity context) {
        ButterKnife.bind(context);

    }

    public TitleUtils(View view) {
        ButterKnife.bind(this, view);
    }

    /**
     * 设置title的背景色
     *
     * @param imageId
     * @return
     */
    public TitleUtils setBackground(int imageId) {
        titleBarView.setBackgroundResource(imageId);
        return this;
    }

    /**
     * 设置左边按钮图片
     *
     * @param imageId
     * @return
     */
    public TitleUtils setLeftImage(int imageId) {
        leftImageView.setVisibility(imageId > 0 ? View.VISIBLE : View.GONE);
        leftImageView.setImageResource(imageId);
        return this;
    }

    /**
     * 设置右侧文本 在本项目没有用到
     *
     * @param text
     * @return
     */
    public TitleUtils setRightText(String text) {
        rightTextView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        rightTextView.setText(text);
        return this;
    }

    /**
     * 设置右侧图片
     *
     * @param imageId
     * @param width   宽度 0为不设置
     * @return
     */
    public TitleUtils setRightImage(int imageId, int width) {
        rightTextView.setVisibility(imageId > 0 ? View.VISIBLE : View.GONE);
        rightTextView.setBackgroundResource(imageId);

//        if (width > 0) {
//            rightTextView.getLayoutParams().width = DensityUtils.dip2px(rightTextView.getContext(), width);
//            rightTextView.getLayoutParams().height = DensityUtils.dip2px(rightTextView.getContext(), width);
//        }
        return this;
    }

    public View getRightView() {
        return rightTextView;
    }

    /**
     * 设置标题
     *
     * @param text
     * @return
     */
    public TitleUtils setTitleText(String text) {
        titleTextView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        titleTextView.setText(text);
        return this;
    }

    /**
     * 设置左侧监听
     *
     * @param listener
     * @return
     */
    public TitleUtils setLeftOnClick(View.OnClickListener listener) {
        if (leftLinearLayout.getVisibility() == View.VISIBLE) {
            leftLinearLayout.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 设置右侧监听
     *
     * @param listener
     * @return
     */
    public TitleUtils setRightOnClick(View.OnClickListener listener) {
        if (rightTextView.getVisibility() == View.VISIBLE) {
            rightTextView.setOnClickListener(listener);
        }
        return this;
    }

    //右侧顶部菜单跳转到home界面
    public void startMainActivity(final Context context) {
        rightTextView.setVisibility(View.VISIBLE);
        rightTextView.setBackgroundResource(R.drawable.ic_main_home_normal);
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.startActivity(context);
            }
        });
    }

    /**
     * 设置标题底部黄色线隐藏和显示
     *
     * @param visibility
     */
    public void setLineVisibility(int visibility) {
        lineView.setVisibility(visibility);
    }

    /**
     * 设置标题左侧按钮隐藏和显示
     *
     * @param visibility
     */
    public void setLeftVisibility(int visibility) {
        leftLinearLayout.setVisibility(visibility);
    }
}
